package com.glodon.paas.account.dao.mysql;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.account.dao.UserDao;
import com.glodon.paas.account.dao.domain.UserEntity;
import com.glodon.paas.account.dao.mapper.UserEntityMapper;

public class UserDaoMysqlImpl implements UserDao {
	private static Logger LOGGER = LoggerFactory.getLogger(UserDaoMysqlImpl.class);

	@Autowired
	private UserEntityMapper userEntityMapper;

	@Override
	public String insert(UserEntity entity) {
		userEntityMapper.insertSelective(entity);
		return entity.getId();
	}

	@Override
	public UserEntity get(String id) {
		return userEntityMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public int delete(String id) {
		return userEntityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public UserEntity getByAccountName(
			String userId, 
			//String loginName, 
			String email,
			String mobile, 
			Integer status) {		
		Map<String, Object> param = new HashMap<String, Object>();
		if(userId!=null){
			param.put("userId", userId);
		}else if(email!=null){
			param.put("email", email);
		}else if(mobile!=null){
			param.put("mobile", mobile);
		} else{
			return null;
		}
		param.put("status", status);
		
		
		List<UserEntity> list = userEntityMapper.selectByWhereAnd(param);
		if (list.size() == 0) {
			return null;
		}
		if (list.size() > 1) {
			// TODO 报警,数据有冲突
			LOGGER.warn("duplication UserEntity, email=" + email);
		}
		return list.get(0);
	}
 
	@Override
	public List<UserEntity> getByIds(String[] userIds) {	 
		return userEntityMapper.selectByPrimaryKeys(userIds);
	}
	
	@Override
	public void update(UserEntity record) {
		userEntityMapper.updateByPrimaryKeySelective(record);
	}

	//@Override
	public void updatePassword(UserEntity user) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", user.getId());
		param.put("password", user.getPasswordSalt());
		param.put("passwordSalt", user.getPasswordSalt());
		userEntityMapper.updateByPrimaryKeyMap(param);
	}
	@Override
	public void addProjectRole(String id,String userId, String projectId, String roleId,String orgId) {
	    userEntityMapper.addProjectRole(id,userId, projectId, roleId,orgId);
	}
	public void setUserEntityMapper(UserEntityMapper userEntityMapper) {
        this.userEntityMapper = userEntityMapper;
    }

	@Override
	public int deleteProjectRole(String userId, String projectId, String roleId,String orgId) {
		return userEntityMapper.deleteProjectRole(userId, projectId, roleId,orgId);
		
	}

	@Override
	public int updateProjectRole(String projectId, String oldeRoleId,
			String newRoleId) {
		return userEntityMapper.updateProjectRole(projectId, oldeRoleId,newRoleId);
	}


//	@Override
//	public boolean checkValidEmail(String email) {
//		List<UserEntity> list = userEntityMapper.selectByEmail(email, null);
//		for (UserEntity user : list) {
//			if (!AccountConstant.USER_STATUS_INACTIVE.equals(user.getStatus())) {
//				return true;
//			}
//		}
//		return false;
//	}
}
